// 定义字段接口
type Field = {
    inputFieldName: string;
    inputFieldIdentifier: string;
    inputFieldType: string;
    inputFieldFormat: string;
    inputFieldValueMapping: string;
    inputFieldValueMappingObject: string;
    sameFieldName: string;
    sameFieldIdentifier: string;
    outputFieldName: string;
    outputFieldIdentifier: string;
    outputFieldType: string;
    outputFieldFormat: string;
    outputFieldDefaultValue: string;
    outputValueFromInput: string;
}

/**
 * 获取字段类型
 */
function getFieldType(value: any): string {
    if (value === null || value === undefined) return 'string';
    if (typeof value === 'number') {
        return Number.isInteger(value) ? 'int' : 'float';
    }
    if (typeof value === 'boolean') return 'boolean';
    if (value instanceof Date) return 'date';
    return 'string';
}

/**
 * 解析JSON对象为字段列表
 */
export function parseJsonToFields(json: any, parentPath = ''): Field[] {
    const fields: Field[] = [];

    Object.entries(json).forEach(([key, value]) => {
        const identifier = parentPath ? `${parentPath}.${key}` : key;
        
        // 如果是对象，递归解析
        if (value && typeof value === 'object' && !(value instanceof Date) && !Array.isArray(value)) {
            fields.push(...parseJsonToFields(value, identifier));
            return;
        }

        const field: Field = {
            inputFieldName: key,
            inputFieldIdentifier: identifier,
            inputFieldType: getFieldType(value),
            inputFieldFormat: '',
            inputFieldValueMapping: 'none',
            inputFieldValueMappingObject: 'none',
            sameFieldName: '1',
            sameFieldIdentifier: '1',
            outputFieldName: key,
            outputFieldIdentifier: identifier,
            outputFieldType: getFieldType(value),
            outputFieldFormat: '',
            outputFieldDefaultValue: '',
            outputValueFromInput: 'input'
        };

        fields.push(field);
    });

    return fields;
}